<?php

class CDemandReply extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;
	
	
	public function __construct(){
		
		$this->_name=R_PRE . "demand_replys";
		
		parent::__construct();
		
	}
	
	public function createReply($data){
		
		$replyId=$this->insert($data);

		return $replyId;		

	}
	
	public function getReplyCounts($fieldArray=null){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,'COUNT(*)');
		
		if($fieldArray!=null){
		
			foreach ($fieldArray as $field => $value){
				
				$where = $db->quoteInto("$field = ?", $value);
				
				$select->where($where);
				
			}
		
		}
		
		return $db->fetchOne($select);
		
		
	}
	
	public function getReplys($from,$counts,$itemid){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,"*");
			
		$select->join(R_PRE.'products',R_PRE.'products.itemid='.$this->_name.'.sell_itemid',array('itemid','cut_url'));
		
		$select->join(R_PRE.'items',R_PRE.'items.itemid='.R_PRE.'products.itemid',array('subject'));
		
		$select->where($this->_name.'.itemid=?',$itemid);
		
		$select->limit($counts,$from);
		
		$thisArray=$db->fetchAll($select);
		
		return $thisArray;
		
	}
	
	public function getReplyByUidCounts($uid){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,"COUNT(*)");
		
		$select->where('reply_uid=?',$uid);
		
		return $db->fetchOne($select);
		
	}
	
	public function getReplysByUid($from,$counts,$uid){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,"*");
		
		$select->where($this->_name.'.reply_uid='.$uid);
		
		$select->joinLeft(R_PRE.'items',R_PRE.'items.itemid='.$this->_name.'.itemid','subject');
		
		$select->joinLeft(R_PRE.'products',R_PRE.'products.itemid='.$this->_name.'.sell_itemid','cut_url');
		
		$select->joinLeft(R_PRE.'members',R_PRE.'items.uid='.R_PRE.'members.uid','username');
		
		$select->joinLeft(R_PRE.'items AS B','B.itemid='.$this->_name.'.sell_itemid','subject AS subject2');
		
		$select->limit($counts,$from);
		
		return $db->fetchAll($select);
		
	}
	
	public function delReplys($field,$valueArray){
		
		$db=$this->getAdapter();
		
		$valueString=join(',',$valueArray);

		$db->delete($this->_name,"$field IN ($valueString)");
		
	}
	
	

	
}